﻿using CsQuery;
using CsQuery.ExtensionMethods;
using System.ComponentModel;

namespace ParseHtml
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var url = "https://www.cnblogs.com/cate/108698/";
            var httpClient = new HttpClient();
            var html = DownloadHtml(httpClient, url).GetAwaiter().GetResult();
            var rs = ParseHtmlAsCnblog(html);
            foreach (var n in rs)
            {
                Console.WriteLine($"{n.Title}---{n.Url}");
            }

        }

        static async Task<string> DownloadHtml(HttpClient http, string url)
        {
            return await http.GetStringAsync(url);
        }

        static List<News> ParseHtmlAsCnblog(string html)
        {
            var result = new List<News>();
            var jq = CQ.CreateDocument(html);
            jq["article .post-item-text a.post-item-title"].ForEach(x =>
            {
                var news = new News
                {
                    Title = x.TextContent,
                    Url = x.GetAttribute("href"),
                };
                result.Add(news);
            });

            return result;
        }
    }

    class News
    {
        public string Title { get; set; }
        public string Url { get; set; }
    }
}
